package benworks.bookcode.wsjc.suggestion120;

/**
 * @author Ben
 */
public class MultiThread implements Runnable {
	int a = 0;

	@Override
	public void run() {
		// 同步代码块,保证原子操作
		synchronized ("") {
			// 自增
			a++;
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			a--;
			String tn = Thread.currentThread().getName();
			System.out.println(tn + ":a = " + a);
		}

	}

	public static void main(String[] args) {
		MultiThread thread = new MultiThread();
		Thread t1 = new Thread(thread);
		// 启动t1线程
		t1.start();

		for (int i = 0; i < 5; i++) {
			new Thread(thread).start();
		}

		// 停止t1线程,破坏了 原子性
		t1.stop();
	}
}
